package com.zjdiepu.www.biz.base;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.SysRoleMapper;
import com.zjdiepu.www.ibiz.base.ISysRoleService;
import com.zjdiepu.www.model.base.dto.SysRole;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.base.ext.SysRoleExt;

/**
 * 角色表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月05日 16时02分<br/>
 * @since 1.0<br/>
 */
@Service
public class SysRoleServiceImpl extends BaseService implements ISysRoleService {
	public static final Logger logger = LoggerFactory.getLogger(SysRoleServiceImpl.class);

	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	@Override
	public void saveUpdateAndDelete(SysRole sysRole, OperateType operType) {
		switch (operType) {
		case add:
			sysRole.initInsertData(sysRole.getModifier(), sysRole.getModifierName());
			sysRoleMapper.insert(sysRole);
			break;
		case edit:
			sysRole.initUpdateData(sysRole.getModifier(), sysRole.getModifierName());
			// 影响的记录数
			int updateCount = sysRoleMapper.updateByPrimaryKeySelective(sysRole);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			sysRole.initDeleteData(sysRole.getModifier(), sysRole.getModifierName());
			// 影响的记录数
			int deleteCount = sysRoleMapper.updateByPrimaryKeySelective(sysRole);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<SysRole> selectByExt(SysRoleExt sysRoleExt) {
		return sysRoleMapper.selectByExt(sysRoleExt);
	}
	
	@Override
	public int countByExt(SysRoleExt sysRoleExt){
		return sysRoleMapper.countByExt(sysRoleExt);
	}
	
	@Override
	public SysRole selectById(String id) {
		return sysRoleMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			sysRoleMapper.clearEmpty(fieldNames, id);
		}
	}

	@Override
	public Set<String> selectByUserName(String userName) {
		return sysRoleMapper.selectByUserName(userName);
	}

	@Override
	public List<SysRole> selectByUserId(String userId) {
		return sysRoleMapper.selectByUserId(userId);
	}
	
	@Override
	public List<SysUser> selectByRoleId(String roleId) {
		return sysRoleMapper.selectByRoleId(roleId);
	}
	
	@Override
	public int countRefined(SysRole sysRole) {
		return sysRoleMapper.countRefined(sysRole);
	}

	@Override
	public SysRole selectRefined(SysRole sysRole) {
		return sysRoleMapper.selectRefined(sysRole);
	}

	@Override
	public boolean repeat(SysRole sysRole) {
		int count = countRefined(sysRole);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(sysRole.getId())) {
			return true;
		}
		SysRole dbItem = selectRefined(sysRole);
		if(null == dbItem) {
			return false;
		}
		return !sysRole.getId().equals(dbItem.getId());
	}
}
